package com.jshx.sz.gdjt.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.NumberKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.activity.listener.MyTextWatcher;
import com.jshx.sz.gdjt.client.ClientApplication;
import com.jshx.sz.gdjt.control.TextEditView;
import com.jshx.sz.gdjt.db.DataBaseUtil;
/**
 * 基础Fragment
 * @author xiangchangan
 * @time 2013-08-22
 */
public class BaseFrame extends Fragment 
{
	//装置编号
	private TextEditView deviceNumber;
	
	//装置型号
	private TextEditView deviceType;
	
	//版本号
	private TextEditView vertionNum;
	
	//生产厂家
	private TextEditView productName;
	
	//作业日期
	private TextEditView workDate;
	
	//额定电流
	private TextEditView ratedCurrent;
	
	//额定电压
	private TextEditView ratedVoltage;
	
	//安装地点
	private TextEditView installAddress;
	
	//页面数据
	private HashMap<String, String> data;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.base_fragment, null);
		initView(view);
		return view;
	}
	
	/**
	 * 组件实例化和初始化
	 * @param view
	 */
	private void initView(View view) {
		deviceNumber = (TextEditView)view.findViewById(R.id.device_number);
		deviceType = (TextEditView)view.findViewById(R.id.device_type);
		vertionNum = (TextEditView)view.findViewById(R.id.version_num);
		productName = (TextEditView)view.findViewById(R.id.product_name);
		workDate = (TextEditView)view.findViewById(R.id.work_date);
		ratedCurrent = (TextEditView)view.findViewById(R.id.rated_current);
		ratedVoltage = (TextEditView)view.findViewById(R.id.rated_voltage);
		installAddress = (TextEditView)view.findViewById(R.id.install_address);
		deviceNumber.setTitle("装置编号");
		deviceType.setTitle("装置型号");
		vertionNum.setTitle("版本号");
		productName.setTitle("生产厂家");
		workDate.setTitle("作业日期");
		ratedCurrent.setTitle("额定电流");
		ratedVoltage.setTitle("额定电压");
		installAddress.setTitle("安装地点");
		
		//设置 额定电流  只能输入特定的值
		DigitsKeyListener numericOnlyListener = new DigitsKeyListener(false,true);
		//设置上限
		ratedCurrent.getEdit().setKeyListener(numericOnlyListener); 
		MyTextWatcher tw1=new MyTextWatcher(ratedCurrent.getEdit());
		ratedCurrent.getEdit().addTextChangedListener(tw1);
		
		//设置 额定电压  只能输入特定的值
		ratedVoltage.getEdit().setKeyListener(numericOnlyListener);
		//设置上限
		MyTextWatcher tw2=new MyTextWatcher(ratedVoltage.getEdit());
		ratedVoltage.getEdit().addTextChangedListener(tw2);
		
		data = ((TaskActivityGroup)getActivity()).getDataFragment1();
		String type = ((TaskActivityGroup)getActivity()).getType();
		if(type != null && "check".equals(type))
		{
			deviceNumber.setValue("编号NO_10-" + ((TaskActivityGroup)getActivity()).getOrderNum());
			deviceType.setValue("型号M_20-" + ((TaskActivityGroup)getActivity()).getOrderNum());
			vertionNum.setValue("版本号V1.2-" + ((TaskActivityGroup)getActivity()).getOrderNum());
			productName.setValue("电信-" + ((TaskActivityGroup)getActivity()).getOrderNum());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			workDate.setValue("" + sdf.format(new Date()));
			installAddress.setValue("广济路-" + ((TaskActivityGroup)getActivity()).getOrderNum());
		}
		//ratedCurrent.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
		//ratedVoltage.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
		
		//二维码扫描到得信息不能修改
		deviceNumber.getEdit().setEnabled(false);
		deviceType.getEdit().setEnabled(false);
		vertionNum.getEdit().setEnabled(false);
		productName.getEdit().setEnabled(false);
		installAddress.getEdit().setEnabled(false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initViewData();
	}

	/**
	 * 获得页面数据
	 * @return
	 */
	public HashMap<String, String> getData() 
	{
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		data.put(DataBaseUtil.task_detail_type2_table_clos[2], deviceNumber.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[3], deviceType.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[4], vertionNum.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[5], productName.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[6], workDate.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[7], ratedCurrent.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[8], ratedVoltage.getEditValue());
		data.put(DataBaseUtil.task_detail_type2_table_clos[9], installAddress.getEditValue());
		return data;
	}

	/**
	 * 设置页面数据
	 * @param data
	 */
	public void initViewData() {
		if(data != null)
		{
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[2]) != null)
			{
				deviceNumber.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[2]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[3]) != null)
			{
				deviceType.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[3]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[4]) != null)
			{
				vertionNum.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[4]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[5]) != null)
			{
				productName.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[5]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[6]) != null)
			{
				workDate.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[6]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[7]) != null)
			{
				ratedCurrent.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[7]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[8]) != null)
			{
				ratedVoltage.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[8]));
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[9]) != null)
			{
				installAddress.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[9]));
			}
		}
	}
}
